﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using _619BJP.Database;
using _619BJP.Models;
using _619BJP.Database.admin;


namespace _619BJP.Web.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        public static int loginUser_id;

        adminloginDB adminloginDb = new adminloginDB();
        productuploadDB productuploadDb = new productuploadDB();
        branduploadDB branduploadDb = new branduploadDB();
        articleuploadDB articleDB = new articleuploadDB();

        public ActionResult adminlogin()
        {
            return View();
        }
        [HttpPost]
        public JsonResult adminloginCheck()
        {
            reJsonResult result = new reJsonResult();

            string username = Request.Params["name"];
            string password = Request.Params["password"];

            loginUser_id = adminloginDb.CheckAdmin(username, password);
            if (loginUser_id>0)
            {
                result.Success = true;
                Session["AdminUser"] = username;

                adminloginDb.AdminLoginUpdate(username);
            }
            else
            {
                result.Success = false;
                result.info = "用户名或密码错误";
            }

          
            return Json(result);
        
        
        }

       
        public ActionResult adminLogout()
        {

            Session.Remove("AdminUser");
         


            return RedirectToAction("adminlogin", "Admin");

        }

        public ActionResult Index()
        {

            if (Session["AdminUser"] == null)
            {
                return RedirectToAction("adminlogin", "Admin");
            }
            else
            {
                return View();
            }
        }

        public ActionResult productupload()
        {
            if (Session["AdminUser"] == null)
            {
                return RedirectToAction("adminlogin", "Admin");
            }
            else
            {
                return View();
            }        
        }
       
        [HttpPost]
        public ActionResult productupload(ProductModel model)
        {
            string imageUrl = "";
            if (Request.Files["img_url"] != null && Request.Files["img_url"].ContentLength > 0)
            {

                string url = Server.MapPath("/ProductImages/");

                HttpPostedFileBase file = Request.Files["img_url"];
                string fileName = string.Format("{0:yyyyMMddmmssss}", DateTime.Now) + file.FileName.Substring(file.FileName.LastIndexOf('.'));

                Request.Files["img_url"].SaveAs(url + fileName);
                imageUrl = "/ProductImages/" + fileName;

            }
            model.img_url = imageUrl;
            productuploadDb.uploadProduct(model);
            return View();
        }

        public ActionResult brandupload()
        {
            return View();
        }
        [HttpPost]
        public ActionResult brandupload(BrandModel model)
        {
            string imageUrl = "";
            if (Request.Files["logo"] != null && Request.Files["logo"].ContentLength > 0)
            {

                string url = Server.MapPath("/BrandLogo/");

                HttpPostedFileBase file = Request.Files["logo"];
                string fileName = string.Format("{0:yyyyMMddmmssss}", DateTime.Now) + file.FileName.Substring(file.FileName.LastIndexOf('.'));

                Request.Files["logo"].SaveAs(url + fileName);
                imageUrl = "/BrandLogo/" + fileName;

            }
            model.logo = imageUrl;

            branduploadDb.uploadBrand(model);



            return View();
        }

        public ActionResult articleupload()
        {
            if (Session["AdminUser"] == null)
            {
                return RedirectToAction("adminlogin", "Admin");
            }
            else
            {
                return View();
            }
        }

        [ValidateInput(false)]
        public JsonResult NewArticle()
        {
            reJsonResult result = new reJsonResult();
            this.ValidateRequest = false;
            string title = Request["article_title"];
            string category = Request["category"];
            string content = Request["content"].ToString();
            int uId = 0;
            if (loginUser_id > 0)
            {
                uId = loginUser_id;
            }

            ArticleModel articelModel = new ArticleModel();
            articelModel.title = title;
            articelModel.category_id = Int32.Parse(category);
            articelModel.content = content;
            articelModel.created_by = uId;

            int article = articleDB.uploadArticle(articelModel);
            if (article>0)
            {
                result.Success = true;
                result.info = "上传成功";
            }
            else
            {
                result.Success = false;
                result.info = "上传失败";
            }

            return Json(result);
        }
    }


    public struct reJsonResult
    {
        public string info { get; set; }
        public bool Success { get; set; }

    }
}
